//
//  ZCEssenceViewController.m
//  BaiSi
//
//  Created by 张灿 on 16/4/24.
//  Copyright © 2016年 张灿. All rights reserved.
//

#import "ZCEssenceViewController.h"
#import "ZCAllViewController.h"
#import "ZCVideoViewController.h"
#import "ZCVoiceViewController.h"
#import "ZCPictureViewController.h"
#import "ZCTextViewController.h"
#import <MJRefresh/MJRefresh.h>

static NSString * const ID = @"cell";

@interface ZCEssenceViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) UICollectionView *collectionView;

@property (weak, nonatomic) UIScrollView *scrollView;

@property (nonatomic, strong) NSMutableArray *titleButtons;

@property (weak, nonatomic) UIButton *selButton;

@property (weak, nonatomic) UIView *underLine;

@end

@implementation ZCEssenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNavBar];
    //加载底部view
    [self setupBottomView];
    
    //加载顶部view
    [self setupTopView];
    
    //添加所有子控制器
    [self setupAllChildViewController];
    //设置所有标题
    [self setupAllTitle];
    
    // iOS7之后，如果是导航控制器下scrollView,顶部都会添加64的额外滚动区域
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (NSMutableArray *)titleButtons
{
    if (_titleButtons == nil) {
        _titleButtons = [NSMutableArray array];
    }
    return _titleButtons;
}

#pragma mark - 添加所有子控制器
- (void)setupAllChildViewController
{
    //全部
    ZCAllViewController *allVC = [[ZCAllViewController alloc] init];
    allVC.title = @"全部";
    [self addChildViewController:allVC];
    
    //视频
    ZCVideoViewController *videoVC = [[ZCVideoViewController alloc] init];
    videoVC.title = @"视频";
    [self addChildViewController:videoVC];
    
    //声音
    ZCVoiceViewController *voiceVC = [[ZCVoiceViewController alloc] init];
    voiceVC.title = @"声音";
    [self addChildViewController:voiceVC];
    
    //图片
    ZCPictureViewController *pictureVC = [[ZCPictureViewController alloc] init];
    pictureVC.title = @"图片";
    [self addChildViewController:pictureVC];
    
    //段子
    ZCTextViewController *textVC = [[ZCTextViewController alloc] init];
    textVC.title = @"段子";
    [self addChildViewController:textVC];
}

#pragma mark - 设置所有标题
- (void)setupAllTitle
{
    NSInteger count = self.childViewControllers.count;
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = ZCScreenW / count;
    CGFloat h = self.scrollView.frame.size.height;
    
    for (int i = 0; i < count; i++) {
        UIViewController *vc = self.childViewControllers[i];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:vc.title forState:UIControlStateNormal];
        button.tag = i;
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        x = i * w;
        button.frame = CGRectMake(x, y, w, h);
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button];
        [self.titleButtons addObject:button];
        //默认选中第一个按钮
        if (i == 0) {
            [self selectButton:button];
            //创建下划线
            UIView *underline = [[UIView alloc]init];
            underline.backgroundColor = [UIColor redColor];
            underline.width = [vc.title sizeWithFont:[UIFont systemFontOfSize:15]].width;
            underline.height = 2;
            underline.centerX = button.centerX;
            underline.y = _scrollView.height - underline.height;
            [self.scrollView addSubview:underline];
            _underLine = underline;
        }
    }
}

#pragma mark - 标题点击时调用
- (void)buttonClick:(UIButton *)button
{
    
    
    NSInteger i = button.tag;
    
    //重复点击标题刷新界面
    if (button == _selButton) {
        UITableViewController *vc = self.childViewControllers[i];
        [vc.tableView.mj_header beginRefreshing];
    }
    
    //一定要放在重复点击下面执行
    [self selectButton:button];
    
    //跳转到对应的子控制器
    self.collectionView.contentOffset = CGPointMake(i * ZCScreenW, 0);
    
}

- (void)selectButton:(UIButton *)button
{
    [UIView animateWithDuration:0.25 animations:^{
        _underLine.centerX = button.centerX;
    }];
    _selButton.selected = NO;
    button.selected = YES;
    _selButton = button;
}


#pragma mark - 搭建底部view:collection
- (void)setupBottomView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.itemSize = CGSizeMake(ZCScreenW, ZCScreenH);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    //取消点击返回顶部
    collectionView.scrollsToTop = NO;
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.pagingEnabled = YES;
    collectionView.showsHorizontalScrollIndicator = NO;
    
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
    [self.view addSubview:collectionView];
    
    _collectionView = collectionView;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.childViewControllers.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    //移除之前的所有的控制器
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    //获取对应的控制器
    UIViewController *vc = self.childViewControllers[indexPath.row];
    
    vc.view.frame = CGRectMake(0, 0, ZCScreenW, ZCScreenH);
    
    [cell.contentView addSubview:vc.view];
    
    return cell;
}

#pragma mark - 搭建顶部view:scrollView
- (void)setupTopView
{
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    //取消标题栏点击回顶部
    scrollView.scrollsToTop = NO;
    CGFloat y = self.navigationController.navigationBarHidden ? 20 : 64;
    scrollView.frame = CGRectMake(0, y, ZCScreenW, 44);
    scrollView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.7];
    [self.view addSubview:scrollView];
    
    _scrollView = scrollView;
}

#pragma mark - 设置导航栏按钮
- (void)setupNavBar
{
    //左
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"nav_item_game_icon"] highlightImage:[UIImage imageNamed:@"nav_item_game_click_icon"] Target:self action:@selector(game)];
    //右
     self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"navigationButtonRandom"] highlightImage:[UIImage imageNamed:@"navigationButtonRandomClick"] Target:self action:@selector(jump)];
    //中
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
}

#pragma mark - 滚动完成调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger i = scrollView.contentOffset.x / ZCScreenW;
    [self selectButton:self.titleButtons[i]];
}

- (void)game
{
    NSLog(@"游戏");
}

- (void)jump
{
    NSLog(@"回到过去");
}



@end
